
package javanlp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class accessPostgres {
    public static void main(String[] args) throws Exception{
        int count=0, s1;
        String s2;
        Class.forName("org.postgresql.Driver"); 
        String qur = "select id, text from java1";
        String url = "jdbc:postgresql://localhost/postgres";
        String user = "postgres", passwd = "cinvprobit";
        PreparedStatement pst;
        Connection con = DriverManager.getConnection(url,user,passwd);
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(qur);
        
        String stm = "INSERT INTO java2(id, text) VALUES(?,?)";
        pst = con.prepareStatement(stm);        
        
        while (rs.next()){
            s1 = rs.getInt("id");
            s2 = rs.getString("text");
            pst.setInt(1, s1);
            pst.setString(2, s2);  
            pst.executeUpdate();            
            count++;
        }
        if (rs != null) {     rs.close();       }
        if (con != null) {    con.close();      }
        System.out.println("There are "+ count + " records inserted.");
    }
}
      